package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.doris.Book;
import com.example.demo.repository.doris.BookRepository;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;
      @Override
    public Book getBookByIsbn(String isbn) {
        return bookRepository.getBookByIsbn(isbn);
    }
    
    @Override
    public List<Book> searchByTitle(String title) {
        return bookRepository.findByTitleContaining(title);
    }
    
    @Override
    public List<Book> searchByAuthor(String author) {
        return bookRepository.findByAuthorContaining(author);
    }    @Override
    public Page<Book> searchBooks(int page, int size, String title, String author, String publisher) {
        // 查询条件
        StringBuilder condition = new StringBuilder();
        if (title != null && !title.isEmpty()) {
            condition.append(" AND title LIKE '%").append(title).append("%'");
        }
        if (author != null && !author.isEmpty()) {
            condition.append(" AND author LIKE '%").append(author).append("%'");
        }
        if (publisher != null && !publisher.isEmpty()) {
            condition.append(" AND publisher LIKE '%").append(publisher).append("%'");
        }
        
        // 先获取总记录数
        String countSql = "SELECT COUNT(*) FROM books WHERE 1=1" + condition;
        Long total = bookRepository.selectCount(countSql);
        
        // 创建分页对象并设置总记录数
        Page<Book> pageParam = new Page<>(page, size);
        pageParam.setTotal(total);
        
        // 计算总页数
        long pages = (total + size - 1) / size; // 改进总页数计算逻辑
        pageParam.setPages(pages);
        
        // 执行分页查询
        Page<Book> result = bookRepository.findBooks(pageParam, title, author, publisher);
        
        // 确保分页信息正确
        if (result.getTotal() != total) {
            result.setTotal(total);
            result.setPages(pages);
        }
        
        return result;
    }    @Override
    public Page<Book> searchByFullText(String keyword, int page, int size) {
        // 先获取总记录数
        String countSql = "SELECT COUNT(*) FROM books WHERE title MATCH_ALL '" + keyword + "'";
        Long total = bookRepository.selectCount(countSql);
        
        // 创建分页对象并设置总记录数
        Page<Book> pageParam = new Page<>(page, size);
        pageParam.setTotal(total);
        
        // 计算总页数
        long pages = (total + size - 1) / size; // 改进总页数计算逻辑
        pageParam.setPages(pages);
        
        // 执行分页查询
        Page<Book> result = bookRepository.searchByFullText(pageParam, keyword);
        
        // 确保分页信息正确
        if (result.getTotal() != total) {
            result.setTotal(total);
            result.setPages(pages);
        }
        
        return result;
    }
}
